package org.csu.springjpetstore.service;

import org.csu.springjpetstore.domain.Category;
import org.csu.springjpetstore.domain.Item;
import org.csu.springjpetstore.domain.Product;
import org.csu.springjpetstore.persistence.CategoryDao;
import org.csu.springjpetstore.persistence.ItemDAO;
import org.csu.springjpetstore.persistence.ProductDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @projectName: MyWebProject
 * @package: org.petstore.service
 * @className: CatalogService
 * @author: Eric
 * @description: TODO
 * @date: 2021/10/31 14:41
 * @version: 1.0
 */
@Service
public class CatalogServiceImpl {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private ProductDAO productDAO;
    @Autowired
    private ItemDAO itemDAO;

    public List<Category> getCategoryList() {
        return categoryDao.getCategoryList();
    }

    public Category getCategory(String categoryId) {
        return categoryDao.getCategory(categoryId);
    }

    public Product getProduct(String productId) {
        return productDAO.getProductByProductId(productId);
    }


    public List<Product> getProductListByCategory(String categoryId) {
        return productDAO.getProductListByCategory(categoryId);
    }

    /**
     * description 模糊查找 对字符串进行小写和%包裹处理 TODO enable using more than one keyword
     *
     * @param keyword:
     * @return List<Product>
     * @author huangzhangyan
     * @date 2021/11/1 0:00
     */

    public List<Product> searchProductList(String keyword) {
        return productDAO.searchProductList("%" + keyword.toLowerCase() + "%");
    }


    public List<Item> getItemListByProduct(String productId) {
        List<Item> itemList = itemDAO.getItemListByProduct(productId);
        return itemList;
    }

    public Item getItem(String itemId) {
        return itemDAO.getItem(itemId);
    }


    public boolean isItemInStock(String itemId) {
        return itemDAO.getInventoryQuantity(itemId) > 0;
    }


    public String findProductListAjax(String keyword) {
        List<Product> productList = searchProductList(keyword);
        String res = "";
        for (int i = 0; i < productList.size(); i++) {
            if (i > 0) {
                res += "," + productList.get(i).getName();
            } else {
                res += productList.get(i).getName();
            }
        }
        return res;
    }
}
